package com.xzkydz.sensor.sensor;


import com.xzkydz.sensor.sensor.base.BaseSensorData;
import com.xzkydz.sensor.utils.CalculationUtils;
import com.xzkydz.sensor.utils.RandomUtils;

/**
 * 差压传感器
 */
public class ChaYaBean extends BaseSensorData {

    /** 压力值数组，长度为5 */
    public Float[] yaLiArray = new Float[5];
    /** 平均压力值 */
    public Float yaLiVel = 0F;


    public ChaYaBean() {
    }


    // TODO: 2022/9/29 协议不明确，此处的解析有可能错误
    @Override
    protected void calculate(byte[] recData) {
        super.calculate(recData);
        float fraction = 1000f; //分数
        int symbol0 = bRec[14]; //符号位
        int symbol1 = bRec[17];
        int symbol2 = bRec[20];
        int symbol3 = bRec[23];
        int symbol4 = bRec[26];

        yaLiArray[0] = CalculationUtils.twoByteToIntNoSign(bRec[15],bRec[16]) / fraction;
        yaLiArray[1] = CalculationUtils.twoByteToIntNoSign(bRec[18],bRec[19]) / fraction;
        yaLiArray[2] = CalculationUtils.twoByteToIntNoSign(bRec[21],bRec[22]) / fraction;
        yaLiArray[3] = CalculationUtils.twoByteToIntNoSign(bRec[24],bRec[25]) / fraction;
        yaLiArray[4] = CalculationUtils.twoByteToIntNoSign(bRec[27],bRec[28]) / fraction;

        if (symbol0 == 1)  yaLiArray[0] = 0 -  yaLiArray[0];
        if (symbol1 == 1)  yaLiArray[1] = 0 -  yaLiArray[1];
        if (symbol2 == 1)  yaLiArray[2] = 0 -  yaLiArray[2];
        if (symbol3 == 1)  yaLiArray[3] = 0 -  yaLiArray[3];
        if (symbol4 == 1)  yaLiArray[4] = 0 -  yaLiArray[4];

        yaLiVel =  (yaLiArray[0] + yaLiArray[1] + yaLiArray[2] + yaLiArray[3] + yaLiArray[4]) / 5f;

        // 格式化
        yaLiVel =  Float.parseFloat(String.format( fraction == 1000f ?  "%.3f" : "%.2f", yaLiVel));

    }

    @Override
    protected void simulation(int sensorNum) {
        super.simulation(sensorNum);
        yaLiVel = RandomUtils.getInstance().randomFloat(20f);
        for (int i = 0; i < yaLiArray.length ; i++) {
            yaLiArray[i] = RandomUtils.getInstance().randomFloat(20f);
        }
    }
}
